package com.cql.cqaudio;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.cql.cqaudio.bean.TimeInfoBean;
import com.cql.cqaudio.listener.WlOnTimeInfoListener;
import com.cql.cqaudio.muterenum.MuterEnum;

import javax.xml.datatype.Duration;
import javax.xml.transform.Source;

/**
 * Created by Chen qin lang on 2018/7/6.
 */

public class Wlplay {
    private static final String TAG = "123";

    static {
        System.loadLibrary("avutil-55");
        System.loadLibrary("swresample-2");
        System.loadLibrary("avcodec-57");
        System.loadLibrary("avformat-57");
        System.loadLibrary("swscale-4");
        System.loadLibrary("postproc-54");
        System.loadLibrary("avfilter-6");
        System.loadLibrary("avdevice-57");
        System.loadLibrary("Wlplay-lib");
    }

    OnPreparedListener onPreparedListener;
    WlOnTimeInfoListener wlOnTImeinfoListener;
    public static TimeInfoBean timeInfoBean;
    public static String source;
    public static boolean playNext = false;
    public static int duration = -1;
    public static int volume = 100;
    public static float pitch = 1;
    public static float tempo = 1;
    public MuterEnum muterEnum = MuterEnum.MUTER_CENTER;

    public static int getVolume() {
        return volume;
    }

    public native int onPrepared(String url);

    public native int start();

    public native int wlResume();

    public native int wlPause();

    private native int n_stop();

    private native void n_setVolume(int process);

    public native int n_seek(int seconds);

    public native int n_getDuration();

    private native int n_muter(int channels);

    private native void n_pitch(float n_pitch);

    private native void n_tempo(float n_tempo);

    public void setPitchAndTempo(float pitch,float tempo) {
        this.pitch=pitch;
        this.tempo=tempo;
        n_pitch(pitch);
        n_tempo(tempo);
    }

    public void set_muter(MuterEnum muter) {
        muterEnum = muter;
        n_muter(muter.getChannels());
    }

    public int getDuration() {
        if (duration < 0) {
            duration = n_getDuration();
        }
        return duration;
    }

    public void prepared(int type) {
        Log.e(TAG, "prepared: ");
        if (onPreparedListener != null) {
            onPreparedListener.onPrepared();
        }
    }

    public void error() {
        Log.e(TAG, "prepared: ");
        if (onPreparedListener != null) {
            onPreparedListener.error();
        }
    }


    public void setOnPreparedListener(OnPreparedListener onPreparedListener) {
        this.onPreparedListener = onPreparedListener;
    }

    public void onCallLoad(boolean isLoad) {
        Log.e(TAG, "onCallLoad: ");
        if (onPreparedListener != null) {
            onPreparedListener.onLoad();
        }
    }

    public void resume() {
        wlResume();
        if (onPreparedListener != null) {
            onPreparedListener.resume();
        }
    }


    public void setWlOnTImeinfoListener(WlOnTimeInfoListener wlOnTImeinfoListener) {
        this.wlOnTImeinfoListener = wlOnTImeinfoListener;
    }

    public void onCallTimeInfo(int current, int tatolTime) {
        if (wlOnTImeinfoListener != null) {
            if (timeInfoBean == null) {
                timeInfoBean = new TimeInfoBean();
            }
            timeInfoBean.setCurrentTime(current);
            timeInfoBean.setTatolTime(tatolTime);
            wlOnTImeinfoListener.onTimeLister(timeInfoBean);
        }
    }

    public void onCallComplete() {
        if (onPreparedListener != null) {
            onPreparedListener.onComplete();
        }
    }

    public void pause() {
        wlPause();
        if (onPreparedListener != null) {
            onPreparedListener.pause();
        }
    }

    public void stop() {
        timeInfoBean = null;
        duration = -1;
        new Thread(new Runnable() {
            @Override
            public void run() {
                n_stop();
            }
        }).start();
    }

    public void setVolume(int process) {
        volume = process;
        n_setVolume(process);
    }


    public void seek(int seconds) {
        n_seek(seconds);

    }

    public void onCallNextAudio() {
        Log.e(TAG, "onCallNextAudio: ");
        if (playNext) {
            playNext = false;
            onPrepared(source);
        }
    }

    public void next(String url) {
        playNext = true;
        source = url;
        stop();

    }

    public void loadStart() {

        setVolume(volume);
        //setPitchAndTempo(pitch,tempo);
        start();

    }

    interface OnPreparedListener {
        void onPrepared();

        void error();

        void onLoad();

        void resume();

        void pause();

        void stop();

        void onComplete();
    }
}
